package com.gogowan.agroenterprise.newview;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gogowan.agroenterprise.LoginActivity;
import com.gogowan.agroenterprise.MyApplication;
import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.base.BaseActivity;
import com.gogowan.agroenterprise.contorl.MyAsyncTask;
import com.gogowan.agroenterprise.model.EditPwdRequest;
import com.gogowan.agroenterprise.model.LoginInfo;
import com.gogowan.agroenterprise.model.LoginResultInfo;
import com.gogowan.agroenterprise.model.Message;
import com.gogowan.agroenterprise.model.ResultInfo;
import com.gogowan.agroenterprise.utils.Utils;
import com.lidroid.xutils.cache.MD5FileNameGenerator;

import org.json.JSONException;
import org.json.JSONObject;

public class ChangePasswordActivity extends BaseActivity {
	private EditText username;
	private EditText password;
	private String name, pwd;
	private TextView login;
	
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_password);
		username = (EditText) findViewById(R.id.login_et_name);
		password = (EditText) findViewById(R.id.login_et_pwd);
		login = (TextView) findViewById(R.id.change_password_wc_btn);

		findViewById(R.id.change_password_backBtn).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						ChangePasswordActivity.this.finish();
					}
				});

		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				name = username.getText().toString();
				pwd = password.getText().toString();

				if (Utils.isEmpty(name) || Utils.isEmpty(pwd)) {
					Toast.makeText(ChangePasswordActivity.this, "请输入密码信息",
							Toast.LENGTH_LONG).show();
				} else if (!name.equals(pwd)) {
					Toast.makeText(ChangePasswordActivity.this, "秘密信息不一致",
							Toast.LENGTH_LONG).show();
				} else {

					EditPwdRequest editPwdRequest = new EditPwdRequest();
					editPwdRequest.setUserId(MyApplication.mSettings.getLong("personid",0));
					editPwdRequest.setOldpassword(Utils.md5Java(MyApplication.mSettings.getString("password","")));
					editPwdRequest.setPassword(Utils.md5Java(name));
					String generate = new MD5FileNameGenerator().generate(name);
					LoginAsyn loginAsyn = new LoginAsyn();
					loginAsyn.execute(editPwdRequest);

					progressDialog = ProgressDialog.show(
							ChangePasswordActivity.this,
							getString(R.string.alert_title),
							getString(R.string.alert_message), true, false);
				}

			}
		});

	}

	/**
	 * 异步登录
	 */
	private class LoginAsyn extends MyAsyncTask {

		@Override
		protected void onPostExecute(Object res) {
			if (res == null || res.equals("")) {
				Toast.makeText(ChangePasswordActivity.this,
						getString(R.string.server_error), Toast.LENGTH_LONG)
						.show();
			} else {
				String data="";
				try {
					JSONObject jo1=new JSONObject(res+"");
					int code = jo1.getInt("code");
					data = jo1.getJSONObject("data").toString();
					Toast.makeText(ChangePasswordActivity.this,
									jo1.getJSONObject("data").getString("message"), Toast.LENGTH_LONG)
							.show();
					if (code != 200||data==null||"{}".equals(data)) {
						return;
					}
					boolean isSavePwd = MyApplication.mSettings.getBoolean("isSavePwd", false);
					MyApplication.editor.putString("password", pwd);
					if (isSavePwd) {
						String json = MyApplication.mSettings.getString("userPwd", "");
						if (json == null || json.equals("")) {

						} else {
							LoginInfo loginInfo = new LoginInfo();
							LoginInfo info = (LoginInfo) Utils.fromJSON(
									Utils.decrypt(json), LoginInfo.class);
							loginInfo.setUsername(info.getLoginName());
							loginInfo.setLoginName(info.getLoginName());
							loginInfo.setPassword(pwd);
							MyApplication.editor.putString("userPwd",
									Utils.encrypt(Utils.toJSON(loginInfo)));
						}
					}
					MyApplication.editor.commit();


					setResult(1000);
					ChangePasswordActivity.this.finish();
				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
			progressDialog.dismiss();
			super.onPostExecute(res);
		}

		@Override
		protected Object doInBackground(Object... params) {
			EditPwdRequest lInfo = (EditPwdRequest) params[0];
			String res = Utils.postCommit(getString(R.string.first_url)
					+ getString(R.string.updatePwd), lInfo);


			return res;
		}
	}
}
